package com.landiao.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.landiao.entities.SysConfig;
import com.landiao.mapper.SysConfigMapper;
@Service
public class SysConfigService extends BaseService<SysConfig, String>{
	
	//系统配置数据加载到内存
	private Map<String, SysConfig> sysConfigMap;

	@Override
	protected SysConfigMapper getBaseMapper() {
		// TODO Auto-generated method stub
		return (SysConfigMapper)baseMapper;
	}
	
	public SysConfig getOneBySysConfig(SysConfig record){
		return this.getBaseMapper().selectOne(record);
	}
	
	public List<SysConfig> getAll(){
		return this.getBaseMapper().queryByPage(new SysConfig(), null);
	}
	
	public Map<String, SysConfig> getSysConfigMap(){
		if(this.sysConfigMap == null){
			this.sysConfigMap = collection2map(this.getAll());
		}
		return this.sysConfigMap;
	}
	
	public void initSysConfigMap(){
		this.sysConfigMap = collection2map(this.getAll());
	}
	
	private static Map<String, SysConfig> collection2map(List<SysConfig> list) {
		if (null == list)
			return null;
		Map<String, SysConfig> map = new HashMap<String, SysConfig>();
		if (list.size() > 0) {
			Iterator<SysConfig> it = list.iterator();
			while (it.hasNext()) {
				SysConfig obj = it.next();
				map.put(obj.getItem(), obj);	
			}
		}
		return map;
	}

	@Override
	public void save(SysConfig record) {
		// TODO Auto-generated method stub
		super.save(record);
		this.initSysConfigMap();
	}

	@Override
	public void update(SysConfig record) {
		// TODO Auto-generated method stub
		super.update(record);
		this.initSysConfigMap();
	}

	@Override
	public void delete(String id) {
		// TODO Auto-generated method stub
		super.delete(id);
		this.initSysConfigMap();
	}
}
